package org.tsinghua.answer.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.tsinghua.answer.domain.entity.PaginationInfo;
import org.tsinghua.answer.domain.question.QuestionStatus;
import org.tsinghua.answer.domain.subject.Subject;
import org.tsinghua.answer.domain.subject.SubjectRepository;
import org.tsinghua.answer.domain.user.User;
import org.tsinghua.answer.domain.user.UserService;

@Controller
public class CustomerController {
	
	@Autowired
	private UserService userService;
	@Autowired
	private SubjectRepository subjectRepository;
//	@Autowired
//	private QuestionService questionService;
//	private DeviceResolver deviceResolver = new LiteDeviceResolver();

	@RequestMapping(value="/", method = RequestMethod.GET)
	public ModelAndView home(@RequestParam(value = "keyword", required=false) String keyword
			, @RequestParam(value = "page", defaultValue="0")Integer page
			, @RequestParam(value="size", defaultValue="20")Integer size
			, @RequestParam(value="status", defaultValue="WAITING") QuestionStatus status, HttpServletRequest request) {
		ModelAndView view = new ModelAndView("redirect:/ts/index");
		return view;
//		Device device = deviceResolver.resolveDevice(request);
//		if(device.isNormal()) {
//			ModelAndView view = new ModelAndView("redirect:/ts/index");
//			return view;
//		}
//		if(!AuthUtils.isAuthenticated()) {
//			throw new SessionAuthenticationException("log in to access this page.");
//		}
//		User user = AuthUtils.getUser();
//		if(user.getType()!=UserType.USER) {
//			return new ModelAndView("redirect:/" + user.getType().toString().toLowerCase());
//		}
//		String username = user.getNickname();
//		if(username == null) {
//			username = user.getUsername();
//		}
//		Page<Question> questions = questionService.userQuestions(user.getId(), keyword, status, page, size);					
//		PaginationInfo pagination = new PaginationInfo(questions.getTotalPages(), questions.getNumber());
//		ModelAndView view = new ModelAndView("home");
//		view.addObject("username", username);
//		view.addObject("questions", questions);
//		view.addObject("status", status.toString());
//		view.addObject("pagination", pagination);
//		return view;
	}
	
	@GetMapping("volunteers")
	public ModelAndView volunteers(@RequestParam(value = "keyword", required=false) String keyword
			, @RequestParam(value = "page", defaultValue="0")Integer page
			, @RequestParam(value="size", defaultValue="20")Integer size) {
		ModelAndView view = new ModelAndView("volunteers");
		Page<User> volunteers = userService.volunteers(keyword, page, size);
		List<Subject> subjects = subjectRepository.findAll();
		PaginationInfo pagination = new PaginationInfo(volunteers.getTotalPages(), volunteers.getNumber());
		view.addObject("volunteers", volunteers);
		view.addObject("subjects", subjects);
		view.addObject("pagination", pagination);
		return view;
	}
	
	@GetMapping("look")
	public ModelAndView look() {
		return new ModelAndView("look");
	}
}
